package com.xc.content.service;

import com.xc.content.bo.CourseTeacherBO;
import com.xc.content.constant.ExceptionCode;
import com.xc.content.convert.CourseTeacherServiceConverter;
import com.xc.content.entity.CourseTeacher;
import com.xc.content.exception.BusinessException;
import com.xc.content.exception.DatabaseException;
import com.xc.content.exception.SystemException;
import com.xc.content.mapper.CourseTeacherMapper;
import com.xc.content.utils.SnowflakeUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import javax.annotation.Resource;
import java.util.Date;

/**
 * @Author: hyacinth
 * @CreateTime: 2025-01-09
 * @Version: 1.0
 */
@Service("courseTeacherService")
@Slf4j
public class CourseTeacherService {

    @Resource
    private CourseTeacherMapper courseTeacherMapper;


    /**
     * 新增课程老师
     *
     * @param courseTeacherBO
     * @return
     */
    public Boolean addCourseTeacher(CourseTeacherBO courseTeacherBO) {
        if (ObjectUtils.isEmpty(courseTeacherBO)) {
            log.error("courseTeacherBO为空");
            throw new BusinessException(ExceptionCode.FIVE_HUNDRED_CODE, "courseTeacherBO为空", courseTeacherBO);
        }
        try {
            CourseTeacher courseTeacher = CourseTeacherServiceConverter.INSTANCE.courseTeacherBoToEntity(courseTeacherBO);
            String id = SnowflakeUtil.nextId();
            courseTeacher.setId(id);
            courseTeacher.setCreateDate(new Date());
            Integer addCourseTeacherCount = courseTeacherMapper.addCourseTeacher(courseTeacher);
            if (addCourseTeacherCount <= 0) {
                log.error("课程老师新增失败");
                throw new DatabaseException(ExceptionCode.FIVE_HUNDRED_CODE, "课程老师新增失败");
            }
            return true;
        } catch (SystemException e) {
            throw new SystemException(ExceptionCode.FIVE_HUNDRED_CODE, "接口内部异常", e);
        }
    }

    /**
     * 修改课程老师
     *
     * @param courseTeacherBO
     * @return
     */
    public Boolean updateCourseTeacher(CourseTeacherBO courseTeacherBO) {
        if (StringUtils.isBlank(courseTeacherBO.getId())) {
            log.error("id不能为空");
            throw new BusinessException(ExceptionCode.FIVE_HUNDRED_CODE, "id不能为空", courseTeacherBO);
        }
        try {

            CourseTeacher courseTeacher = CourseTeacherServiceConverter.INSTANCE.courseTeacherBoToEntity(courseTeacherBO);
            CourseTeacher courseTeacher1 = courseTeacherMapper.queryByCourseId(courseTeacher.getCourseId(), courseTeacher.getId());
            if (ObjectUtils.isEmpty(courseTeacher1)) {
                log.error("对应课程id的课程没有课程老师");
                throw new BusinessException(ExceptionCode.FIVE_HUNDRED_CODE, "对应课程id的课程没有课程老师", courseTeacherBO);
            }
            Integer updateCourseTeacherCount = courseTeacherMapper.updateCourseTeacher(courseTeacher);
            if (updateCourseTeacherCount <= 0) {
                log.error("课程老师修改失败");
                throw new DatabaseException(ExceptionCode.FIVE_HUNDRED_CODE, "课程老师修改失败");
            }
            return true;
        } catch (SystemException e) {
            throw new SystemException(ExceptionCode.FIVE_HUNDRED_CODE, "接口内部异常", e);
        }
    }
}
